package pharmacy.view;

import pharmacy.Pharmacy;
import pharmacy.model.Concelho;
import pharmacy.model.District;
import pharmacy.model.Farmacia;
import pharmacy.model.Farmacias;
import swing.Dialog;

import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.io.IOException;
import java.util.Vector;

import static java.awt.Color.black;

public class PharmacyDialog extends Dialog {

    /**
     *
     */
    private static final long serialVersionUID = 2342491344703528140L;
    private final LocalidadePanel localidadePanel = new LocalidadePanel();

    public PharmacyDialog() {
        super(Flag.PACKED, Flag.CENTERED);

        getContentPane().setLayout(new BorderLayout());

        getContentPane().add(localidadePanel, BorderLayout.NORTH);
        getContentPane().add(new FarmaciasTabbedPane());
    }

    private class LocalidadePanel extends JPanel {
        /**
         *
         */
        private static final long serialVersionUID = -7946859228149745410L;

        private final DistrictsComboBox districtsComboBox = new DistrictsComboBox();
        private final ConcelhosComboBox concelhosComboBox = new ConcelhosComboBox();

        private LocalidadePanel() {
            setLayout(new GridLayout(0, 1));
            add(districtsComboBox);
            add(concelhosComboBox);
        }

        private class DistrictsComboBox extends JComboBox<District> {
            /**
             *
             */
            private static final long serialVersionUID = -7928967795198275595L;

            private DistrictsComboBox() {
                setModel(new DefaultComboBoxModel<>(new Vector<>(Pharmacy.DISTRICTS.values())));
            }
        }

        private class ConcelhosComboBox extends JComboBox<Concelho> {
            /**
             *
             */
            private static final long serialVersionUID = -1493755828622607864L;

            private ConcelhosComboBox() {
                districtsComboBox.addActionListener(e -> {
                    removeAll();
                    final District district = (District) districtsComboBox.getSelectedItem();

                    if (district == null)
                        return;

                    try {
                        setModel(new DefaultComboBoxModel<>(district.getConcelhos()));
                    } catch (IOException e1) {
                        throw new RuntimeException(e1);
                    }
                });
            }
        }
    }

    private class FarmaciasTabbedPane extends JTabbedPane {
        /**
         *
         */
        private static final long serialVersionUID = -975308603904959120L;

        private final FarmaciasGrid permanenteGrid = new FarmaciasGrid();
        private final FarmaciasGrid disponibilidadeGrid = new FarmaciasGrid();

        private FarmaciasTabbedPane() {
            add(permanenteGrid, "Permanente");
            add(disponibilidadeGrid, "Disponibilidade");

            localidadePanel.concelhosComboBox.addActionListener(e -> {
                permanenteGrid.removeAll();
                disponibilidadeGrid.removeAll();

                final Concelho concelho = (Concelho) localidadePanel.concelhosComboBox.getSelectedItem();
                if (concelho == null)
                    return;

                try {
                    Farmacias[] farmacias = concelho.getFarmacias();
                    permanenteGrid.setFarmacias(farmacias[0]);
                    disponibilidadeGrid.setFarmacias(farmacias[1]);
                } catch (IOException e1) {
                    throw new RuntimeException(e1);
                }
            });
        }

        private class FarmaciasGrid extends JPanel {
            /**
             *
             */
            private static final long serialVersionUID = -2247974554795614573L;

            private FarmaciasGrid() {
                super(new GridLayout(0, 1));
            }

            public void setFarmacias(Farmacias farmacias) {
                removeAll();
                for (Farmacia farmacia : farmacias)
                    add(new FarmaciaPanel(farmacia));
            }

            private class FarmaciaPanel extends JPanel {
                /**
                 *
                 */
                private static final long serialVersionUID = -3434714975810496169L;

                public FarmaciaPanel(Farmacia farmacia) {
                    setBorder(new TitledBorder(new LineBorder(black), farmacia.getNome()));
                    setLayout(new GridLayout(0, 1));
                    for (String linha : farmacia.getContato())
                        add(new JLabel(linha));
                }
            }
        }
    }
}
